package com.example.tys.oomdata;

import android.os.Bundle;
import android.os.Debug;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.tys.oomdata.sqLiteData.DaoFactory;
import com.example.tys.oomdata.sqLiteData.DaoSqlImpl;
import com.example.tys.oomdata.sqLiteData.DaoSqlProxy;
import com.example.tys.oomdata.sqLiteData.DbHelper;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author tys
 */
public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        Debug.stopMethodTracing();
    }


    public void bt(View view) {
        //create table if not exists TEST(aDouble double ,aString text ,anInt integer ,anLong bigint )
        //create table if not exists tb_user(_id INTEGER,name TEXT,password TEXT)
        DaoFactory daoFactor = new DaoFactory();
        final DaoSqlProxy<Test, DaoSqlImpl<Test>> testDao = daoFactor.getDaoSqLite(Test.class);
        final DaoSqlProxy<Ben, DaoSqlImpl<Ben>> benDao = daoFactor.getDaoSqLite(Ben.class);
        for (int i = 0; i < 1; i++) {

            //noinspection AlibabaAvoidManuallyCreateThread
            new Thread(new Runnable() {
                @Override
                public void run() {
                    long start = System.currentTimeMillis();
                    ArrayList<Test> list = new ArrayList<Test>();
                    ArrayList<Ben> l = new ArrayList<Ben>();
                    for (int i = 0; i < 200; i++) {
                        Test t = new Test();
                        t.setaDouble((double) i);
                        t.setaBoolean(false);
                        t.setaString("中文");
                        testDao.insert(t);
                        benDao.insert(new Ben("{haha}"));
                    }
                    long dif = System.currentTimeMillis() - start;
                    Log.e("----", " " + dif);

                }
            }).start();

        }
        Toast.makeText(this, "ok ", Toast.LENGTH_SHORT).show();
    }

    public void delete(View view) {
        DaoSqlProxy<Test, DaoSqlImpl<Test>> testDao = new DaoFactory().getDaoSqLite(Test.class);
        Test t = new Test();
        t.setAnInt(6);
        long dif = testDao.delete(t);
        Toast.makeText(this, "ok " + dif, Toast.LENGTH_SHORT).show();
    }

    public void updta(View view) {
        DaoSqlProxy<Test, DaoSqlImpl<Test>> dao = new DaoFactory().getDaoSqLite(Test.class);

        Test o = new Test();
        o.setAnInt(1);
        Test n = new Test();
        n.setaString("hello");
        n.setAnLong(12L);
        n.setaBoolean(true);
        n.setaByte("我是誰".getBytes());
        n.setaDouble(123456.123456789d);//1000.12
        n.setaFloat(123456.123456789f);
        short a;
        a = 'a';
        n.setaShort(a);
        long ret = dao.update(o, n);
        Toast.makeText(this, "ok " + ret, Toast.LENGTH_SHORT).show();
    }

    public void qure(View view) {
        DaoSqlProxy<Test, DaoSqlImpl<Test>> dao = new DaoFactory().getDaoSqLite(Test.class);
        Test t = new Test();
        t.setAnLong(999L);
        List<Test> list = dao.query(t, " _Id DESC", 10, 10);
        for (Test a : list) {
            byte[] data = a.getaByte();
            Log.i("---", a.toString()
            );
            if (data != null) {
                Log.i("---", " 数据：" + new String(data));
            }

        }
    }
}
